package com.zhoufeng.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.plugin.activerecord.Model;
import com.zhoufeng.db.ConnectionManager;

public class DeviceConfigure extends Model<DeviceConfigure> {
	private static final long serialVersionUID = 1L;
	public final static DeviceConfigure me = new DeviceConfigure();

	@Override
	public Integer getInt(String attr) {
		// TODO Auto-generated method stub

		if (super.get(attr) == null) {
			return 0;
		}
		return super.getInt(attr);
	}

	public String getConfigureinfotoStr(String id, String msgs) {
		StringBuffer sb = new StringBuffer("AA EA ");
		Connection con = null;
		try {
			String[] ss = msgs.split(" ");
			con = ConnectionManager.getConnection(true);
			PreparedStatement pstmtupdatesetter = null,pstmtupdatesetonoroff = null;
			// 根据发来的数据报更新服务器设置
			String sqlupdatesetter = "update thing_device_info set  model = (case length(model) when 2 then concat('1',?) else ? end ) , "
					+ "functionalswitch = (case length(functionalswitch) when 2 then concat('1',?) else ? end ) , frequency=? "
					+ ",setonoroff=?  where id = ? and aftersetcount > 3";
			
			
			//如果是离线切换到在线状态，自动把开关机状态改成3、链接外网的状态
			String sqlupdatesetonoroff = "update thing_device_info set setonoroff = 3 where (select onoroff from thing_device where id= thing_device_info.id ) = 0 and id = ?";
			pstmtupdatesetonoroff = con.prepareStatement(sqlupdatesetonoroff);
			pstmtupdatesetonoroff.setString(1, ss[19] + ss[18] + ss[17]);
			pstmtupdatesetonoroff.execute();
			pstmtupdatesetonoroff.close();
			
			
			String model10 = new Integer(Integer.parseInt(ss[4], 16)).toString();
			String model = model10.length() > 1 ? model10.substring(1) : model10;
			String functionswitch10 = new Integer(Integer.parseInt(ss[5], 16)).toString();
			String functionswitch = functionswitch10.length() > 1 ? functionswitch10.substring(1) : functionswitch10;
			String frequency = new Integer(Integer.parseInt(ss[2], 16)).toString();

			Integer onoroff = 3;
			
			if("00".equals(ss[2]) && "00".equals(ss[16])){
				onoroff = 1;
				sqlupdatesetter = "update thing_device_info set setonoroff=? where id = ? and aftersetcount > 3";
				pstmtupdatesetter = con.prepareStatement(sqlupdatesetter);
				pstmtupdatesetter.setInt(1, onoroff);
				pstmtupdatesetter.setString(2, ss[19] + ss[18] + ss[17]);
			}else{
				pstmtupdatesetter = con.prepareStatement(sqlupdatesetter);
				pstmtupdatesetter.setString(1, model);
				pstmtupdatesetter.setString(2, model);
				pstmtupdatesetter.setString(3, functionswitch);
				pstmtupdatesetter.setString(4, functionswitch);
				pstmtupdatesetter.setString(5, frequency);
				pstmtupdatesetter.setInt(6, onoroff);
				pstmtupdatesetter.setString(7, ss[19] + ss[18] + ss[17]);
			}
			pstmtupdatesetter.execute();
			pstmtupdatesetter.close();
		} catch (SQLException e1) {
			e1.printStackTrace();
		} finally {
			ConnectionManager.closeConnection(con, null);
		}

		DeviceConfigure config = me.findById(id);

		if (config != null) {
			String s = null;
			Integer count = 0;
			s = Integer.toString(config.getInt("reportinginterval"), 16).toUpperCase();
			count += config.getInt("reportinginterval");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("frequency"), 16).toUpperCase();
			count += config.getInt("frequency");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("model"), 16).toUpperCase();
			count += config.getInt("model");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("functionalswitch"), 16).toUpperCase();
			count += config.getInt("functionalswitch");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			String time = config.getStr("time");

			time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());

			Integer min = new Integer(time.substring(14, 16));
			count += min;
			s = Integer.toString(min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer hour = new Integer(time.substring(11, 13));
			count += hour;
			s = Integer.toString(hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer day = new Integer(time.substring(8, 10));
			count += day;
			s = Integer.toString(day, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer month = new Integer(time.substring(5, 7));
			count += month;
			s = Integer.toString(month, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer year = new Integer(time.substring(2, 4));
			count += year;
			s = Integer.toString(year, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("on_min"), 16).toUpperCase();
			count += config.getInt("on_min");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("on_hour"), 16).toUpperCase();
			count += config.getInt("on_hour");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("off_min"), 16).toUpperCase();
			count += config.getInt("off_min");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("off_hour"), 16).toUpperCase();
			count += config.getInt("off_hour");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("setonoroff"), 16).toUpperCase();
			count += config.getInt("setonoroff");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("openthedustpm25out"), 16).toUpperCase();
			count += config.getInt("openthedustpm25out");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			Integer in1 = Integer.parseInt(id1, 16);
			Integer in2 = Integer.parseInt(id2, 16);
			Integer in3 = Integer.parseInt(id3, 16);
			count += in3;
			count += in2;
			count += in1;

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");

			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}

			sb.append("AB");

//			try {
//				config.set("setonoroff", 3);
//				config.update();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}

		} else {
			Date date = new Date();
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
			int count = 0;
			String s = null;

			int reportinginterval = 5;
			int frequency = 0;
			int model = 1;
			int functionalswitch = 1;
			int on_min = 30;
			int on_hour = 8;
			int off_min = 30;
			int off_hour = 21;
			int setonoroff = 3;
			int openthedustpm25out = 50;

			count += reportinginterval;
			s = Integer.toString(reportinginterval, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += frequency;
			s = Integer.toString(frequency, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += model;
			s = Integer.toString(model, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += functionalswitch;
			s = Integer.toString(functionalswitch, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer min = new Integer(time.substring(14, 16));
			count += min;
			s = Integer.toString(min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer hour = new Integer(time.substring(11, 13));
			count += hour;
			s = Integer.toString(hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer day = new Integer(time.substring(8, 10));
			count += day;
			s = Integer.toString(day, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer month = new Integer(time.substring(5, 7));
			count += month;
			s = Integer.toString(month, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer year = new Integer(time.substring(2, 4));
			count += year;
			s = Integer.toString(year, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += on_min;
			s = Integer.toString(on_min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += on_hour;
			s = Integer.toString(on_hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += off_min;
			s = Integer.toString(off_min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += off_hour;
			s = Integer.toString(off_hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += setonoroff;
			s = Integer.toString(setonoroff, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += openthedustpm25out;
			s = Integer.toString(openthedustpm25out, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			Integer in1 = Integer.parseInt(id1, 16);
			Integer in2 = Integer.parseInt(id2, 16);
			Integer in3 = Integer.parseInt(id3, 16);

			count += in3;
			count += in2;
			count += in1;

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");

			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}

			sb.append("AB");

		}

		return sb.toString();
	}
	public String getConfigureinfotoStrForApp(String id, String msgs) {
		StringBuffer sb = new StringBuffer("40 EA ");
//		Connection con = null;
//		try {
//			String[] ss = msgs.split(" ");
//			con = ConnectionManager.getConnection(true);
//			PreparedStatement pstmtupdatesetter = null;
//			// 根据发来的数据报更新服务器设置
//			String sqlupdatesetter = "update thing_device_info set  model = (case length(model) when 2 then concat('1',?) else ? end ) , "
//					+ "functionalswitch = (case length(functionalswitch) when 2 then concat('1',?) else ? end ) , frequency=? where id = ? and aftersetcount > 3";
//			String model10 = new Integer(Integer.parseInt(ss[4], 16)).toString();
//			String model = model10.length() > 1 ? model10.substring(1) : model10;
//			String functionswitch10 = new Integer(Integer.parseInt(ss[5], 16)).toString();
//			String functionswitch = functionswitch10.length() > 1 ? functionswitch10.substring(1) : functionswitch10;
//			String frequency = new Integer(Integer.parseInt(ss[2], 16)).toString();
//			
//			pstmtupdatesetter = con.prepareStatement(sqlupdatesetter);
//			
//			pstmtupdatesetter.setString(1, model);
//			pstmtupdatesetter.setString(2, model);
//			pstmtupdatesetter.setString(3, functionswitch);
//			pstmtupdatesetter.setString(4, functionswitch);
//			pstmtupdatesetter.setString(5, frequency);
//			pstmtupdatesetter.setString(6, ss[19] + ss[18] + ss[17]);
//			pstmtupdatesetter.execute();
//			pstmtupdatesetter.close();
//		} catch (SQLException e1) {
//			e1.printStackTrace();
//		} finally {
//			ConnectionManager.closeConnection(con, null);
//		}
		
		DeviceConfigure config = me.findById(id);
		
		if (config != null) {
			String s = null;
			Integer count = 0;
			s = Integer.toString(config.getInt("reportinginterval"), 16).toUpperCase();
			count += config.getInt("reportinginterval");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			s = Integer.toString(config.getInt("frequency"), 16).toUpperCase();
			count += config.getInt("frequency");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("model"), 16).toUpperCase();
			count += config.getInt("model");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("functionalswitch"), 16).toUpperCase();
			count += config.getInt("functionalswitch");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			String time = config.getStr("time");
			
			time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());
			
			Integer min = new Integer(time.substring(14, 16));
			count += min;
			s = Integer.toString(min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer hour = new Integer(time.substring(11, 13));
			count += hour;
			s = Integer.toString(hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer day = new Integer(time.substring(8, 10));
			count += day;
			s = Integer.toString(day, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer month = new Integer(time.substring(5, 7));
			count += month;
			s = Integer.toString(month, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer year = new Integer(time.substring(2, 4));
			count += year;
			s = Integer.toString(year, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			s = Integer.toString(config.getInt("on_min"), 16).toUpperCase();
			count += config.getInt("on_min");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("on_hour"), 16).toUpperCase();
			count += config.getInt("on_hour");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			s = Integer.toString(config.getInt("off_min"), 16).toUpperCase();
			count += config.getInt("off_min");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			s = Integer.toString(config.getInt("off_hour"), 16).toUpperCase();
			count += config.getInt("off_hour");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			s = Integer.toString(config.getInt("setonoroff"), 16).toUpperCase();
			count += config.getInt("setonoroff");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			s = Integer.toString(config.getInt("openthedustpm25out"), 16).toUpperCase();
			count += config.getInt("openthedustpm25out");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);
			
			Integer in1 = Integer.parseInt(id1, 16);
			Integer in2 = Integer.parseInt(id2, 16);
			Integer in3 = Integer.parseInt(id3, 16);
			count += in3;
			count += in2;
			count += in1;
			
			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");
			
			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}
			
			sb.append("AB");
			
			try {
				config.set("setonoroff", 3);
				config.update();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		} else {
			Date date = new Date();
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
			int count = 0;
			String s = null;
			
			int reportinginterval = 5;
			int frequency = 0;
			int model = 1;
			int functionalswitch = 1;
			int on_min = 30;
			int on_hour = 8;
			int off_min = 30;
			int off_hour = 21;
			int setonoroff = 3;
			int openthedustpm25out = 50;
			
			count += reportinginterval;
			s = Integer.toString(reportinginterval, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			count += frequency;
			s = Integer.toString(frequency, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			count += model;
			s = Integer.toString(model, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			count += functionalswitch;
			s = Integer.toString(functionalswitch, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer min = new Integer(time.substring(14, 16));
			count += min;
			s = Integer.toString(min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer hour = new Integer(time.substring(11, 13));
			count += hour;
			s = Integer.toString(hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer day = new Integer(time.substring(8, 10));
			count += day;
			s = Integer.toString(day, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer month = new Integer(time.substring(5, 7));
			count += month;
			s = Integer.toString(month, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			Integer year = new Integer(time.substring(2, 4));
			count += year;
			s = Integer.toString(year, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			count += on_min;
			s = Integer.toString(on_min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += on_hour;
			s = Integer.toString(on_hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += off_min;
			s = Integer.toString(off_min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += off_hour;
			s = Integer.toString(off_hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			count += setonoroff;
			s = Integer.toString(setonoroff, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			count += openthedustpm25out;
			s = Integer.toString(openthedustpm25out, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			
			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);
			
			Integer in1 = Integer.parseInt(id1, 16);
			Integer in2 = Integer.parseInt(id2, 16);
			Integer in3 = Integer.parseInt(id3, 16);
			
			count += in3;
			count += in2;
			count += in1;
			
			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");
			
			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}
			
			sb.append("AB");
			
		}
		
		return sb.toString();
	}

	public String getConfigureinfotoStr(String id) {
		StringBuffer sb = new StringBuffer("AA EA ");

		// try {
		// String[] ss = msgs.split(" ");
		// Connection con = ConnectionManager.getConnection(true);
		// PreparedStatement pstmtupdatesetter = null;
		// // 根据发来的数据报更新服务器设置
		// String sqlupdatesetter =
		// "update thing_device_info set  model = (case length(model) when 2 then concat('1',?) else ? end ) , "
		// +
		// "functionalswitch = (case length(functionalswitch) when 2 then concat('1',?) else ? end ) , frequency=? where id = ? and aftersetcount < 3";
		//
		// String model = ss[4].length() > 1 ? ss[4].substring(1) : ss[4];
		// String functionswitch = ss[5].length() > 1 ? ss[5].substring(1) :
		// ss[5];
		// String frequency = new Integer(Integer.parseInt(ss[2],
		// 16)).toString();
		//
		// pstmtupdatesetter = con.prepareStatement(sqlupdatesetter);
		//
		// pstmtupdatesetter.setString(1, model);
		// pstmtupdatesetter.setString(2, model);
		// pstmtupdatesetter.setString(3, functionswitch);
		// pstmtupdatesetter.setString(4, functionswitch);
		// pstmtupdatesetter.setString(5, frequency);
		// pstmtupdatesetter.setString(6, ss[19] + ss[18] + ss[17]);
		// pstmtupdatesetter.execute();
		// pstmtupdatesetter.close();
		// } catch (SQLException e1) {
		// TODO Auto-generated catch block
		// e1.printStackTrace();
		// }

		DeviceConfigure config = me.findById(id);

		if (config != null) {
			String s = null;
			Integer count = 0;
			s = Integer.toString(config.getInt("reportinginterval"), 16).toUpperCase();
			count += config.getInt("reportinginterval");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("frequency"), 16).toUpperCase();
			count += config.getInt("frequency");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("model"), 16).toUpperCase();
			count += config.getInt("model");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("functionalswitch"), 16).toUpperCase();
			count += config.getInt("functionalswitch");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			String time = config.getStr("time");

			time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date());

			Integer min = new Integer(time.substring(14, 16));
			count += min;
			s = Integer.toString(min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer hour = new Integer(time.substring(11, 13));
			count += hour;
			s = Integer.toString(hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer day = new Integer(time.substring(8, 10));
			count += day;
			s = Integer.toString(day, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer month = new Integer(time.substring(5, 7));
			count += month;
			s = Integer.toString(month, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer year = new Integer(time.substring(2, 4));
			count += year;
			s = Integer.toString(year, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("on_min"), 16).toUpperCase();
			count += config.getInt("on_min");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			s = Integer.toString(config.getInt("on_hour"), 16).toUpperCase();
			count += config.getInt("on_hour");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("off_min"), 16).toUpperCase();
			count += config.getInt("off_min");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("off_hour"), 16).toUpperCase();
			count += config.getInt("off_hour");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("setonoroff"), 16).toUpperCase();
			count += config.getInt("setonoroff");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			s = Integer.toString(config.getInt("openthedustpm25out"), 16).toUpperCase();
			count += config.getInt("openthedustpm25out");
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			Integer in1 = Integer.parseInt(id1, 16);
			Integer in2 = Integer.parseInt(id2, 16);
			Integer in3 = Integer.parseInt(id3, 16);
			count += in3;
			count += in2;
			count += in1;

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");

			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}

			sb.append("AB");

			try {
				config.set("setonoroff", 3);
				config.update();
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else {
			Date date = new Date();
			String time = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
			int count = 0;
			String s = null;

			int reportinginterval = 5;
			int frequency = 0;
			int model = 1;
			int functionalswitch = 1;
			int on_min = 30;
			int on_hour = 8;
			int off_min = 30;
			int off_hour = 21;
			int setonoroff = 3;
			int openthedustpm25out = 50;

			count += reportinginterval;
			s = Integer.toString(reportinginterval, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += frequency;
			s = Integer.toString(frequency, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += model;
			s = Integer.toString(model, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += functionalswitch;
			s = Integer.toString(functionalswitch, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer min = new Integer(time.substring(14, 16));
			count += min;
			s = Integer.toString(min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer hour = new Integer(time.substring(11, 13));
			count += hour;
			s = Integer.toString(hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer day = new Integer(time.substring(8, 10));
			count += day;
			s = Integer.toString(day, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer month = new Integer(time.substring(5, 7));
			count += month;
			s = Integer.toString(month, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			Integer year = new Integer(time.substring(2, 4));
			count += year;
			s = Integer.toString(year, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += on_min;
			s = Integer.toString(on_min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += on_hour;
			s = Integer.toString(on_hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += off_min;
			s = Integer.toString(off_min, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}
			count += off_hour;
			s = Integer.toString(off_hour, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += setonoroff;
			s = Integer.toString(setonoroff, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			count += openthedustpm25out;
			s = Integer.toString(openthedustpm25out, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				sb.append(s + " ");
			}

			String id1 = id.substring(0, 2);
			String id2 = id.substring(2, 4);
			String id3 = id.substring(4, 6);

			Integer in1 = Integer.parseInt(id1, 16);
			Integer in2 = Integer.parseInt(id2, 16);
			Integer in3 = Integer.parseInt(id3, 16);

			count += in3;
			count += in2;
			count += in1;

			sb.append(id3 + " ");
			sb.append(id2 + " ");
			sb.append(id1 + " ");

			s = Integer.toString(count, 16).toUpperCase();
			if (s.length() < 2) {
				sb.append("0" + s + " ");
			} else {
				if (s.length() > 2) {
					sb.append(s.substring(s.length() - 2) + " ");
				} else {
					sb.append(s + " ");
				}
			}

			sb.append("AB");

		}

		return sb.toString();
	}

}
